package main

import (
	"log"
	"mygoedu/day11/socket"
	"net"
	"time"
)

//server端
func main1() {
	addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:8080")
	socket.CheckError(err)

	log.Println("listen")
	listener, err := net.ListenTCP("tcp", addr)
	socket.CheckError(err)

	log.Println("waiting for client...")
	conn, err := listener.Accept()
	socket.CheckError(err)
	log.Println("establish connect from: ", conn.RemoteAddr())

	request := make([]byte, 256)
	_, err = conn.Read(request)
	socket.CheckError(err)
	log.Printf("receive from client: %s\n", string(request))

	n, err := conn.Write([]byte("hello" + string(request)))
	socket.CheckError(err)
	log.Printf("send to client: %d\n", n)

	time.Sleep(1 * time.Second)
	_, err = conn.Write([]byte("hello"))
	socket.CheckError(err)
	log.Printf("send to client: %d\n", n)
	conn.Close()
}
